package com.testdocs.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.testdocs.client.project.initialselect.SelectProjectPlace;
import com.testdocs.client.structure.general.GeneralPanels;
import com.testdocs.client.structure.general.GeneralPanelsPresenter;
import com.testdocs.client.structure.menu.MenuPanel;
import com.testdocs.client.structure.menu.MenuPanelPresenter;
import com.testdocs.client.structure.top.TopPanel;
import com.testdocs.client.structure.top.TopPanelPresenter;
import com.testdocs.client.test.menu.TestGroupMenu;
import com.testdocs.client.test.menu.TestGroupMenuPresenter;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test_docs implements EntryPoint {
	
	private Place defaultPlace = new SelectProjectPlace();
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		ClientFactory clientFactory = new ClientFactoryImpl();
        EventBus eventBus = clientFactory.getEventBus();
        PlaceController placeController = clientFactory.getPlaceController();
        
        // Start ActivityManager for the main widget with our ActivityMapper
        ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
        ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
        
        // Start PlaceHistoryHandler with our PlaceHistoryMapper
        AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(placeController, eventBus, defaultPlace);

		IsWidget generalPanels = buildGeneralStructure(clientFactory, activityManager);
		
        RootLayoutPanel.get().add(generalPanels);
        // Goes to the place represented on URL else default place
        historyHandler.handleCurrentHistory();
	}

	private IsWidget buildGeneralStructure(ClientFactory clientFactory, ActivityManager activityManager) {
		GeneralPanels generalPanels = clientFactory.getGeneralPanels();
		generalPanels.setPresenter(new GeneralPanelsPresenter(clientFactory, activityManager));
		
		TopPanel topPanel = clientFactory.getTopPanel();
		topPanel.setPresenter(new TopPanelPresenter(clientFactory));
		topPanel.setStatusBar(clientFactory.getStatusBar());
		generalPanels.setTop(topPanel);
		
		MenuPanel menuPanel = clientFactory.getMenuPanel();
		menuPanel.setPresenter(new MenuPanelPresenter(clientFactory));
		clientFactory.getWorkAreaPanel().setMenu(menuPanel);
		
		TestGroupMenu testGroupMenu = clientFactory.getTestGroupMenu();
		testGroupMenu.setPresenter(new TestGroupMenuPresenter(clientFactory));
		menuPanel.setTestGroupMenu(testGroupMenu);
				
		return generalPanels;
	}
	
}
